#!/usr/bin/env perl
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
# This script is a wrapper exposing the legacy vespa-deploy-application script

use strict;
use warnings;
use Getopt::Long;

my $help=0;
my $debug=0;
my $dryrun=0;
my $force=0;
my $nobackup=0;
my $skip_xml_validation=0;
my $verbose=0;
my $nosudo=0;
my $write=0;
my $outputdir=0;

GetOptions ("h" => \$help,
            "d" => \$debug,
            "n" => \$dryrun,
            "f" => \$force,
            "nobackup" => \$nobackup,
            "skipxmlvalidation" => \$force,
            "v" => \$verbose,
            "nosudo" => \$nosudo,
            "w" => \$write,
            "p" => \$outputdir)
or $help = 1;

if ($help) {
  print_usage();
  exit(0);
}

my $app = shift;

my $flags = " ";
if ($debug) {
  $flags .= "-v "
}
if ($force) {
  $flags .= "-f ";
}
if ($verbose) {
  $flags .= "-v ";
}
if ($dryrun) {
  $flags .= "-n ";
}

print "WARNING: vespa-deploy-application is deprecated, use 'vespa-deploy prepare' instead\n\n";

my $DEPLOY="vespa-deploy $flags prepare $app";

system($DEPLOY);
if ($? == 0) { print "Activate the application by running 'vespa-activate-application'\n"; }

sub print_usage {
  print "\n";
  print "usage: vespa-deploy-application <application-directory>\n";
  print "Switches:\n";
  print "-h This help text\n";
  print "-d Debug (stack trace on exceptions)\n";
  print "-n Dry-run, check application without activating, print the MD5 checksum of the application\n";
#  print "-nobackup Do not backup application package data\n";
#  print "-skipxmlvalidation Skip validation of application package XML files\n";
  print "-v Verbose (progress trace)\n";
#  print "-nosudo Do not sudo as yahoo when deploying\n";
#  print "-w Write .MODEL debugging files when the vespa model is built\n";
  print "-f Force, try to deploy even if package contains errors\n";
#  print "-p <Directory to be used for writing (if using -w) and reading config system files during deploy. Default: $VESPA_HOME/var/db/vespa/config_server/serverdb>\n";
  print "\n";
  print "Verify that the 'yahoo' user has read and execute permissions\n";
  print "to each level of the directory tree from the current directory\n";
  print "down to and including the application directory.\n";
}

